package com.jk.lesson5;

import com.jk.bean.Dish;
import com.jk.lesson4.MenuTest;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;

/**
 * Created by Auser on 2016/11/14.
 */
public class MatchTest {

    private static List<Dish> menu = MenuTest.menu;

    private static List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6);

    public static void main(String...args){
        test_5_3_1();
        test_5_3_2();
        test_5_3_3();
        test_5_3_4();
        test_5_4_1();
    }

    public static void test_5_3_1(){
        boolean has = menu.stream().anyMatch(Dish::isVegetarian);
        boolean all = menu.stream().allMatch(dish -> dish.getCalories() <1000);
        boolean none = menu.stream().noneMatch(dish -> dish.getCalories() <= 1000);

        System.out.println("====>"+has+"====>"+all+"====>"+none);

    }

    public static void test_5_3_2(){
        Optional<Dish> dish = menu.stream()
                .filter(Dish::isVegetarian)
                .findAny();
        System.out.println(dish.toString());
    }

    public static void test_5_3_3(){

        Optional<Integer> first = numbers.stream()
                .map(integer -> integer * integer)
                .filter(integer -> integer % 3 == 0)
                .findAny();
        System.out.println(first);
    }

    /**
     * 求和
     */
    public static void test_5_3_4(){
        int product = numbers.stream().reduce(0, (integer, integer2) -> integer + integer2);
        System.out.println(product);
        Optional<Integer> sum = numbers.stream().reduce((integer, integer2) -> integer + integer2);
        System.out.println(sum);
        String strs = numbers.stream().map(integer -> "" + integer).reduce("", (s, s2) -> s + s2);
        System.out.println(strs);
    }

    /**
     * 最大值
     */
    public static void test_5_4_1(){
        Optional<Integer> max = numbers.stream()
                .reduce(Integer::max);
        System.out.println(max);
    }

    /**
     * 测验5.3规约
     */
    public static void test_5_4_2(){
        menu.stream().count();
    }
}
